package symptomsSearch;

import ctm.javacode.dbBean;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Iterator;
import org.json.*;

import java.net.*;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.BoldStyle;
import jxl.format.Colour;

import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

@WebServlet("/symptomsSearch/semanticNetworkMaitain.do")
public class semanticNetworkMaitain extends HttpServlet {

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/plain");
		response.setCharacterEncoding("UTF-8");
		PrintWriter print = response.getWriter();

		String button_type = request.getParameter("button");

		String str = request.getParameter("str");
		String str2 = request.getParameter("str2");
		String str3 = request.getParameter("str3");
		String id = request.getParameter("id");
		try {
			String Result = "";

			if (button_type.equals("6")) {

				Result = ModifiedStandardSymptoms(str, str2, str3, id);

			} else if (button_type.equals("7")) {

				Result = DeleteStandardSymptoms(id);

			} else if (button_type.equals("8")) {

				Result = AddNewStandardSymptoms(str, str2, str3);

			} else {

				// do nothing

			}

			print.write(Result);
			print.flush();
			print.close();

		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	} // end doPost

	private static String ModifiedStandardSymptoms(String a, String b,
			String c, String id) throws NamingException, SQLException {
		dbBean db = new dbBean("jdbc/ctmpllab");
		db.Connected();

		String sqlQuery_update_to_database = "UPDATE standard_table SET name = '"
				+ a + "', parent_id = '" + b + "', class = '" + c + "' WHERE id = " + id + "";

		try {
			db.ExcuteUpdate(sqlQuery_update_to_database);

		} catch (SQLException e) {
			throw new SQLException(e);
		} // end try-catch
		db.CloseConn();
		return "success";

	}

	private static String AddNewStandardSymptoms(String a, String b, String c)
			throws NamingException, SQLException {
		dbBean db = new dbBean("jdbc/ctmpllab");
		db.Connected();

		String sqlQuery_insert_to_database = "INSERT INTO standard_table (name,parent_id,class) VALUE ('"
				+ a + "','" + b + "','" + c + "')";

		try {
			db.ExcuteUpdate(sqlQuery_insert_to_database);

		} catch (SQLException e) {
			throw new SQLException(e);
		} // end try-catch
		db.CloseConn();
		return "success";

	}

	private static String DeleteStandardSymptoms(String id)
			throws NamingException, SQLException {
		dbBean db = new dbBean("jdbc/ctmpllab");
		db.Connected();
		String sqlQuery_delete_from_database = "DELETE FROM standard_table  WHERE id ='"
				+ id + "'";

		try {

			db.ExcuteUpdate(sqlQuery_delete_from_database);

		} catch (SQLException e) {
			throw new SQLException(e);
		} // end try-catch
		db.CloseConn();
		return "success";

	}

}
